<?php
class Recover extends cl_BasePage {

	public $indexsearchform;
	public $new_passw;
	public $form_new_passw;
	public $formrecover;
	public $msg;	
	
	function __construct() {
		parent::__construct();
		//parent::doInit();
		
		$this->title = "Login | CulturaLive.com | it_IT";
	}	
		
	
	function doInit() {
		parent::doInit();
		global $teardrop;			
	
		$this->indexsearchform = new Header_IndexSearchform($teardrop->sysroot."/app/header/indexsearchform.phtml");
		$this->subheader->loginform = $this->indexsearchform;		
		
		$utility = new cl_Utility();		
		
		if ($utility->get_arg("key") && $utility->get_arg("id")) {			
			$this->createform_new_passw();
		} else {			
			$this->createform();			
		}		
		
		if ($utility->get_arg("sendmail") == "sent") {			
			$this->msg = "<div class=\"ext sub_header_gray\"><p>A mail was sent to you to regenerate your password</p></div>";
		} else if ($utility->get_arg("recoverok") == "ok") {
			$this->msg = "<div class=\"ext sub_header_gray\"><p>A new password has been generate</p></div>";
		}
	
	}	
	
	
	function createform_new_passw() {
		
		global $webroot;
	  	
		$utility = new cl_Utility();
		
	  	$this->form_new_passw = new TdForm("form_new_passw", "../core/app/form/form.phtml");
		$this->form_new_passw->setName("form_new_passw");
		$this->form_new_passw->setMethod("POST");
		$this->form_new_passw->setAction($webroot."/recover.htm/key/".$utility->get_arg("key")."/id/".$utility->get_arg("id"));	

		$fieldset1 = new TdFieldset("fieldset_signup1", "../core/app/form/fieldset.phtml");
	  	$fieldset1->setName("fieldset_signup1");
	  	$this->form_new_passw->addField($fieldset1);				
		
		$l_password = new TdLabel("label_password", "../core/app/form/label.phtml");
	  	$l_password->setClass("label_signup");
	  	$l_password->setValue("Password");
	
	  	$new_password = new TdPasswordField("new_password", "../core/app/form/passwfield.phtml");
	  	$new_password->setLabel($l_password);
	  	$new_password->setRequired(true);
	  	$new_password->setName("new_password");
	  	$new_password->setSize("13");
	  	$new_password->setClass("input_signup");
	  	$fieldset1->addField($new_password);
	  	
	  	
	  	$l_conf_password = new TdLabel("label_conf_password", "../core/app/form/label.phtml");
	  	$l_conf_password->setClass("label_signup");
	  	$l_conf_password->setValue("Conferma Password");
	
	  	$conf_password = new TdPasswordField("conf_password", "../core/app/form/passwfield.phtml");
	  	$conf_password->setLabel($l_conf_password);
	  	$conf_password->setRequired(true);
	  	$conf_password->setCompare($new_password);
	  	$conf_password->setName("conf_password");
	  	$conf_password->setSize("13");
	  	$conf_password->setClass("input_signup");
	  	$fieldset1->addField($conf_password);
		
		$submit_newpassw = new TdSubmitField("submit_newpassw", "../core/app/form/submit.phtml");
		$submit_newpassw->setValue("");
		$submit_newpassw->setName("submit_newpassw");
		$submit_newpassw->setClass("submit_sign_up_1");
		$fieldset1->addField($submit_newpassw);			
		
				
		$this->addComponent($this->form_new_passw);	  	
	  	
	  }
	
	
	
	
	
	
	
	function createform() {
		
		global $webroot;
	  	
	  	$this->formrecover = new TdForm("form_recover", "../core/app/form/form.phtml");
		$this->formrecover->setName("form_recover");
		$this->formrecover->setMethod("POST");
		$this->formrecover->setAction($webroot."/recover.htm/sendmail/sent");	

		$fieldset1 = new TdFieldset("fieldset_signup1", "../core/app/form/fieldset.phtml");
	  	$fieldset1->setName("fieldset_signup1");
	  	$this->formrecover->addField($fieldset1);
	  	
	  	/*$hidden_recover = new TdHiddenField("hidden_recover", "../core/app/form/hiddenfield.phtml");
		$hidden_recover->setValue("form_recover");
		$hidden_recover->setName("hidden_form");
		$fieldset1->addField($hidden_recover);*/
		
		$loginhtmlcode_rec = new TdHtmlCode("loginhtmlcode", "../core/app/form/htmlcode.phtml");
		//$code2 = "<p class=\"login_text\">Recover your password or "."<a href=\"#\" onclick=\"show_login()\">"."Log in"."</a>"." to start with us"."</p>";
		$code2 = "<p class=\"recover_text\">Recover your password or "."<a href=\"".$webroot."/login.htm\">"."Log in"."</a>"." to start with us"."</p>";
		$loginhtmlcode_rec->setValue($code2);
		$fieldset1->addField($loginhtmlcode_rec);
		
		
		$l_username = new TdLabel("label_username", "../core/app/form/label.phtml");
	  	$l_username->setClass("label_signup");
	  	$l_username->setValue("Username");
	
	  	$username_rec = new TdTextField("username_rec", "../core/app/form/textfield.phtml");
	  	$username_rec->setLabel($l_username);
	  	$username_rec->setRequired(true);
	  	$username_rec->setName("username_rec");
	  	$username_rec->setSize("13");
	  	$username_rec->setClass("input_signup");
	  	$fieldset1->addField($username_rec);
		
		$submit_rec = new TdSubmitField("submit_login_rec", "../core/app/form/submit.phtml");
		$submit_rec->setValue("");
		$submit_rec->setName("login_rec");
		$submit_rec->setClass("submit_sign_up_1");
		$fieldset1->addField($submit_rec);			
		
				
		$this->addComponent($this->formrecover);	  	
	  	
	  }
	
	
	
	function doValidate() {
		global $teardrop;
		
		parent::doValidate();		
		
		if ($this->formrecover) {
		
			if (!$this->formrecover->gotError()) {
				
		  		$res = true;	  		
		  		$res = $res && $username = $this->formrecover->getField("fieldset_signup1")->getField("username_rec")->getValue();
		  		  		
		  		
		  		if ($res) {
		  			$res = $res && $user = new cl_User("username", $username);
	
		  			$idusr = $user->getId();
		  			
		  			if ($res) {
		  				
		  				$utility = new cl_Utility();	
		  				
		  				$res = $res && $key = $utility->get_random_string("abcdefghijklmnopqrstuvwz0123456789", 32);
		  				$res = $res && $this->sql_con->insert("otp (otp.key, id_user) VALUES ('".$key."', ".$idusr.");");
	
		  				if ($res) {
		  					$res = $res && $utility->send_recover_email($key, $idusr);
		  					
		  				}
		  			}	  		
		  		}
		  		
		  		if (!$res) {
		  			trigger_error("Can't send welcome email to new subscriber.", E_USER_ERROR);
		  		}
		  		
		  	}
		  	
		} else if ($this->form_new_passw) {
			
			if (!$this->form_new_passw->gotError()) {
				
				$utility = new cl_Utility();
				
				$res = true;
				$res = $res && $user = new cl_User("id", $utility->get_arg("id"));
				
				if ($res) {					
					$user->setPassword(md5($this->form_new_passw->getField("fieldset_signup1")->getField("new_password")->getValue()));
					$user->save();
					$teardrop->route("recover.htm/recoverok/ok");	  				
				}
				
			if (!$res) {
		  			trigger_error("Can't generate a new passeord.", E_USER_ERROR);
		  		}
				
			
			}
		}
			
	}
	
	
	
}
?>
